package mumu.android.app;

import mumu.android.R;
import mumu.android.lib.Async.Future;
import mumu.android.lib.app.SysReceiver;

import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * 
 * bind服务处理。 关闭activity后，服务终止
 * 
 * @author Administrator
 * 
 */
public class bindservier extends Activity {

	Button bind, unbind, getServiceStatus;
	// 保持所启动的Service的IBinder对象
	zLib_bindservice.MyBinder binder;
	// 定义一个ServiceConnection对象
	private ServiceConnection conn = new ServiceConnection() {
		// 当该Activity与Service连接成功时回调该方法
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			Toast.makeText(getApplicationContext(), "Service Connected", 1000).show();
			// 获取Service的onBind方法所返回的MyBinder对象
			binder = (zLib_bindservice.MyBinder) service;
		}

		// 当该Activity与Service断开连接时回调该方法
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Toast.makeText(getApplicationContext(), "Service Disconnected-", 1000).show();
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("bind服务");
		setContentView(R.layout.broadcast1);

		Button btn_3 = (Button) findViewById(R.id.btn_3);// reg
		Button btn_4 = (Button) findViewById(R.id.btn_4);// unreg
		Button btn_1 = (Button) findViewById(R.id.btn_1);// unreg
		// 创建启动Service的Intent
		final Intent intent = new Intent(this, zLib_bindservice.class);
		// 为Intent设置Action属性
		btn_3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// startService(intent);
				bindService(intent, conn, Service.BIND_AUTO_CREATE);
			}
		});
		btn_4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// stopService(intent);
				unbindService(conn);
			}
		});
		btn_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 获取、并显示Service的count值
				Toast.makeText(bindservier.this, "Serivce的count值为：" + binder.getCount(), 4000).show();
			}
		});
	}

}
